import { defineStore } from "pinia";
import { Alert, phoneReg } from "@/utils/tool";
import { setItem, getItem } from "@/utils/localStorage";

const useAppointmentStore = defineStore("appointmentStore", {
  state: () => {
    return {
      // 套餐ID
      appointmentServiceId: undefined,
      // 预约服务（哪个套餐）
      appointmentService: "",
      // 服务时间
      serviceTime: "",
      // 联系人
      contacts: "",
      // 联系电话
      contactNumber: "",
      // 预约人数
      appointmentQuantity: "",

      // 预约订单列表
      appointmentOrder: getItem("appointmentOrder") || [],
    };
  },
  actions: {
    // 进行校验
    testVal() {
      if (!this.appointmentService) {
        // Alert("请选择服务项目");
        return false;
      }

      if (!this.serviceTime) {
        // Alert("请选择服务时间");
        return false;
      }

      if (!this.contacts) {
        // Alert("请输入联系人");
        return false;
      }

      // if (!this.contactNumber) {
      //   // Alert("请输入联系电话");
      //   return false;
      // } else if (!phoneReg(Number(this.contactNumber))) {
      //   // Alert("联系电话不合法");
      //   return false;
      // }

      if (!this.contactNumber) {
        // Alert("请输入联系电话");
        return false;
      }

      console.log("this.appointmentQuantity:", this.appointmentQuantity);

      if (!this.appointmentQuantity) {
        // Alert("请输入预约人数");
        return false;
      } else if (
        this.appointmentQuantity <= 0 ||
        isNaN(this.appointmentQuantity)
      ) {
        // Alert("预约人数不合法");
        return false;
      }

      this.appointmentQuantity = Number(this.appointmentQuantity);

      return "ok";
    },

    // 添加预约订单
    updateAppointmentOrder(item) {
      this.appointmentOrder.push(item);

      setItem("appointmentOrder", this.appointmentOrder);
    },

    // 删除最后一项
    deleteFinally() {
      this.appointmentOrder.shift();

      setItem("appointmentOrder", this.appointmentOrder);
    },
  },
  getters: {},
});

export default useAppointmentStore;
